package com.jt.tenant.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.prolificinteractive.materialcalendarview.OnMonthChangedListener;
import com.jt.tenant.R;
import com.jt.tenant.activity.TrackActivity;
import com.jt.tenant.adapter.TrackAdapter;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetTravelByDate;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilScreen;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/2/15.
 */
public class TrackDialog extends JTDialog implements OnDateSelectedListener, OnMonthChangedListener {

    private Calendar calendar = Calendar.getInstance();

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    private List<GetTravelByDate.Travel> list = new ArrayList<>();

    private TrackAdapter trackAdapter;

    private MaterialCalendarView kalendar;

    private ListView listView;

    private GetTravelByDate getTravelByDate = new GetTravelByDate(new AsyCallBack<GetTravelByDate.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetTravelByDate.Info info) throws Exception {

            if (type == 0) {

                TrackDialog.super.show();

            }

            if (info.list.size()!= 0) {

                listView.setVisibility(View.VISIBLE);

                list.clear();

                list.addAll(info.list);

                trackAdapter.notifyDataSetChanged();

            }else {

                listView.setVisibility(View.GONE);

            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            if (type == 1 || type == -1) {

                calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + type);

            }

        }

    });

    public TrackDialog(final Context context) {

        super(context);

        setOwnerActivity((Activity) context);

        setContentView(R.layout.dialog_track);

        View.OnClickListener listener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                switch (v.getId()) {

                    case R.id.track_background:

                        dismiss();


                        break;

                }

            }

        };


        findViewById(R.id.track_background).setOnClickListener(listener);

        kalendar = (MaterialCalendarView)findViewById(R.id.calendarView);

        kalendar.state().edit()

                .setMaximumDate(new Date())

                .setMinimumDate(CalendarDay.from(2015, 12, 31))

                .commit();

        kalendar.setTileHeight(50);

        kalendar.setSelectedDate(new Date());

        kalendar.setSelectionColor(Color.parseColor("#ff6565"));

        kalendar.setOnDateChangedListener(this);

        kalendar.setOnMonthChangedListener(this);

        listView = (ListView) findViewById(R.id.track_list_view);

        listView.setAdapter(trackAdapter = new TrackAdapter(context, list) {

            @Override
            protected void onClose(String s) {

                list.remove(s);

                notifyDataSetChanged();

            }

        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                context.startActivity(new Intent(context, TrackActivity.class)

                        .putExtra("id", list.get(position).ID));

            }

        });

    }

    private void getDate(int type) {

        getTravelByDate.date = getSelectedDatesString();

        getTravelByDate.execute(getContext(), type);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Window window = getWindow();

        window.setGravity(Gravity.TOP);

        WindowManager.LayoutParams layoutParams = window.getAttributes();

        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;

        layoutParams.height = UtilScreen.screenSize(getContext())[1] - JTApplication.ScaleScreenHelper.getWidthHeight(200);

    }

    @Override
    public void show() {

        getDate(0);

    }


    @Override
    public void onDateSelected(@NonNull MaterialCalendarView widget, @Nullable CalendarDay date, boolean selected) {

        getDate(0);

    }

    @Override
    public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {

        //noinspection ConstantConditions

    }

    private String getSelectedDatesString() {

        CalendarDay date = kalendar.getSelectedDate();

        if (date == null) {

            return "No Selection";

        }

        return simpleDateFormat.format(date.getDate());

    }
}